<!--
 * @Descripttion: 出手鸭小程序
 * @version:1.0.1 
 * @Author: HHL
 * @Date: 2022-11-15 10:24:46
 * @LastEditTime: 2025-11-05 20:15:00
 * @LastEditors: sueRimn
-->
<template>
	<view class="page">
		<!-- Banner区域 -->
		<view class="banner">
			<image class="banner-image" src="https://image.chushouya.com//upload/20251105/1762348040736_o4qpii.png" mode="widthFix"></image>
		</view>

		<!-- 提示信息 -->
		<view class="notice">
			<image class="notice-icon" src="https://image.chushouya.com//upload/20251105/1762347022623_qdmhdy.png" mode="aspectFit"></image>
			<text class="notice-text">热门机型，每日市场报价！</text>
		</view>

		<!-- 分类和品牌容器 -->
		<view class="content-container">
			<!-- 分类标签 -->
			<view class="category-tabs">
				<view 
					v-for="(category, index) in categories" 
					:key="category.categoryId"
					:class="['tab-item', { active: currentCategoryIndex === index }]"
					@click="switchCategory(index)"
				>
					{{ category.categoryName }}
				</view>
			</view>

			<!-- 品牌网格 -->
			<view class="brand-grid" v-if="currentCategory">
				<view 
					v-for="brand in currentCategory.brands" 
					:key="brand.brandId"
					class="brand-item"
					@click="handleBrandClick(brand)"
				>
					<view class="brand-icon">
						<image 
							v-if="brand.brandImage" 
							:src="brand.brandImage" 
							mode="aspectFit"
							class="brand-logo"
						></image>
						<text v-else class="brand-placeholder">{{ brand.brandName.substring(0, 2) }}</text>
					</view>
					<text class="brand-name">{{ brand.brandName }}</text>
				</view>
			</view>
		</view>

		<!-- 公共底部 -->
		<bottom-nav type='home' />
	</view>
</template>

<script>
import { getDouyinHomeData } from '@/api'

export default {
	name: 'Index',
	components: {

	},
	data() {
		return {
			categories: [],
			currentCategoryIndex: 0,
		}
	},
	computed: {
		currentCategory() {
			return this.categories[this.currentCategoryIndex] || null
		}
	},
	onLoad() {
		uni.hideTabBar()
		this.loadHomeData()
	},
	methods: {
		async loadHomeData() {
			try {
				const res = await getDouyinHomeData()
				if (res.code === 200 && res.data && res.data.categories) {
					this.categories = res.data.categories
				}
			} catch (error) {
				console.error('加载首页数据失败:', error)
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
			}
		},
		switchCategory(index) {
			this.currentCategoryIndex = index
		},
		handleBrandClick(brand) {
			// 如果包含url则跳转到webview
			if (brand.url) {
				const encodedUrl = encodeURIComponent(brand.url)
				uni.navigateTo({
					url: `/pages/webview/index?url=${encodedUrl}`
				})
			} else if (brand.brandImage) {
				// 否则打开品牌图片预览
				uni.previewImage({
					urls: [brand.ossUrl],
					current: brand.ossUrl
				})
			}
		}
	},
	onShareAppMessage() {
		return {
			title: '出手鸭',
			path: '/pages/index/index',
		}
	},
	onShareTimeline() {
		return {
			title: '出手鸭',
		}
	},
}
</script>

<style lang="scss" scoped>
.page {
	// #ifdef MP-TOUTIAO
	padding: 1rpx 0 20rpx 0;
	// #endif
	// #ifndef MP-TOUTIAO
	padding: 1rpx 0 190rpx 0;
	// #endif
	background: linear-gradient(180deg, #ffeac1 0%, #f6f7f9 100%);
	min-height: 100vh;
}

.header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 30rpx;
	background: #fff;
	
	.header-left, .header-right {
		display: flex;
		align-items: center;
		gap: 30rpx;
	}
	
	.header-title {
		font-size: 36rpx;
		font-weight: 600;
		color: #333;
	}
	
	.icon-back, .icon-more, .icon-share {
		font-size: 48rpx;
		color: #333;
	}
}

.banner {
	padding: 0 30rpx;
	margin-top: 20rpx;
	
	.banner-image {
		width: 100%;
		display: block;
	}
}

.notice {
	margin: 20rpx 30rpx;
	padding: 20rpx 30rpx;
	background: #FFF;
	border-radius: 40rpx;
	display: flex;
	align-items: center;
	
	.notice-icon {
		width: 42rpx;
		height: 42rpx;
		margin-right: 10rpx;
	}
	
	.notice-text {
		font-size: 28rpx;
		color: #FF6B35;
	}
}

.content-container {
	width: 690rpx;
	margin: 0 auto;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
}

.category-tabs {
	display: flex;
	padding: 30rpx;
	gap: 20rpx;
	overflow-x: auto;
	white-space: nowrap;
	
	.tab-item {
		font-size: 28rpx;
		color: #666;
		background: #f6f7f9;
		border: 2rpx solid #f6f7f9;
		border-radius: 20rpx;
		flex-shrink: 0;
		height: 80rpx;
		width: 142rpx;
		text-align: center;
		line-height: 80rpx;
		
		&.active {
			color: #333;
			background: #fff7e2;
			font-weight: 600;
			border: 2rpx solid #ffce46;
		}
	}
}

.brand-grid {
	padding: 30rpx;
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	gap: 40rpx 20rpx;
	
	.brand-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.brand-icon {
			width: 100rpx;
			height: 100rpx;
			border-radius: 24rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 16rpx;
			overflow: hidden;
			
			.brand-logo {
				width: 100%;
				height: 100%;
			}
			
			.brand-placeholder {
				font-size: 40rpx;
				font-weight: 600;
				color: #999;
			}
		}
		
		.brand-name {
			font-size: 24rpx;
			color: #333;
			text-align: center;
			max-width: 120rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}
}
</style>
